<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      function myInstanceOf(left, right) {
        let proto = Object.getPrototypeOf(left);
        let prototype = right.prototype;
        while (true) {
          if (!proto) return false;
          if (proto === prototype) return true;
          proto = Object.getPrototypeOf(proto);
        }
      }

      function myInstanceOf(left, right) {
        let proto = left._proto_;
        let prototype = right.prototype;
        while (true) {
          if (!proto) return false;
          if (proto === prototype) return true;
          ptoto = proto._proto_;
        }
      }

      function myNew() {
        let obj = {};
        let fn = Array.prototype.shift.call(arguments);
        obj._proto = fn.prototype;
        let result = fn.call(obj, arguments);
        return typeof result === "object" ? result : obj;
      }

      function objectCreate(obj) {
        function Fn() {}
        Fn.prototype = obj;
        return new Fn();
      }

      function myNew() {
        let obj = null;
        let fn = [].shift.call(arguments);
        obj = Object.create(fn.prototype);
        let resut = fn.call(obj, arguments);
        return typeof resut === "object" ? result : obj;
      }

      function promiseAll(promises) {
        return new Promise((resolve, reject) => {
          if (!Array.isArray(promises)) {
            throw new TypeError("promises must be array");
          }
          var resolvedCounter = 0;
          var promiseLength = promises.length;
          var resolveResult = [];
          for (let i = 0; i < promiseLength; i++) {
            Promise.resolve(promises[i]).then(
              (value) => {
                resolvedCounter++;
                resolveResult[i] = value;
                if (resolvedCounter === promiseLength) {
                  return resolve(resolveResult);
                }
              },
              (err) => {
                return reject(err);
              }
            );
          }
        });
      }

      // let p1 = new Promise((resolve, reject) => {
      //   setTimeout(() => {
      //     resolve(100);
      //   }, 1000);
      // });
      // let p2 = new Promise((resolve, reject) => {
      //   setTimeout(() => {
      //     resolve(200);
      //   }, 2000);
      // });
      // let p3 = new Promise((resolve, reject) => {
      //   setTimeout(() => {
      //     resolve(300);
      //   }, 3000);
      // });

      // promiseAll([p1, p3, p2]).then((res) => {
      //   console.log(res);
      // });

      function promiseAll(promises) {
        return new Promise((resolve, reject) => {
          if (!Array.isArray(promises)) {
            throw new TypeError("promises must be array");
          }
          var counter = 0;
          promiseLength = promises.length;
          resolveResult = [];
          for (let i = 0; i < promiseLength; i++) {
            Promise.resolve(promises[i]).then(
              (value) => {
                counter++;
                resolveResult[i] = value;
                if (counter === promiseLength) {
                  return resolve(resolveResult);
                }
              },
              (err) => {
                return reject(err);
              }
            );
          }
        });
      }

      function promiseRace(promises) {
        return new Promise((resolve, reject) => {
          promises.forEach((p) => {
            Promise.resolve(p).then(
              (value) => {
                resolve(value);
              },
              (err) => {
                reject(err);
              }
            );
          });
        });
      }

      function promiseRace(promises) {
        return new Promise((resolve, reject) => {
          promises.forEach((p) => {
            Promise.resolve(p).then(
              (value) => {
                resolve(value);
              },
              (err) => {
                reject(err);
              }
            );
          });
        });
      }

    </script>
  </body>
</html>
